<?php


class ServiceHandler {

    const PHP_ARRAY = 'php_array';
    const XML = 'xml';
     
    
    private $router=array();
    private $pdio;
    
    function __construct() {
        
    }
    
    
    public function attachPDO($pdo) {
        $this->pdio = $pdo;
    }
    
    public function attachRouter($rtr) {
        $this->router = $rtr;
    }    
    
    public function getRouter() {
        return $this->router;
    }    
    
    public function appendRouter($rtr) {
        $this->router = array_merge($this->router, $rtr);
    }
    
    public function call($name, $data = array(), $data_type=ServiceHandler::PHP_ARRAY) {
        
        $output = array();
        
        if (!$script = $this->route($name)) {
            throw new FWerrorException("service $name not found: routed services:<br><pre>".print_r($this->router, true)."</pre>");
        } else {
            require_once($script);
            $fname='CallService_'.$name;
            $input = self::convert_to_arr($data, $data_type);
            $output = $fname($this->pdio,$input);
        }
        
        return self::convert_arr($output, $data_type);
        
    }
    
    function convert_to_arr($data, $data_type){
        if ($data_type != ServiceHandler::PHP_ARRAY) {
            
        } else {
            $input = $data;    
        }
        
        return $input;
    }
    
    
    function convert_arr($arr, $data_type) {
        return $arr;
    }
    
    
    private function route($sName) {
        return nvli($this->router, $sName, false);
    }
    
}


function call_service($name, $data = array(), $data_type=ServiceHandler::PHP_ARRAY) {
    return $GLOBALS['ServiceHandler']->call($name, $data, $data_type);
}



/***
 * 
 * $res = Service->call($name, $input, $data_type=PHP_ARRAY)
 * 
 * 
 * 
 * 
 * 
 */